package com.base.shiro.web;


import java.util.Map;

import com.base.common.utils.map.MapUtil;
import com.base.shiro.entity.ResModel;
import com.base.shiro.web.validator.ResourceValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * ResourceController
 * @author yuqs
 * @since 0.1
 */
public class ResController extends Controller {
	public void index() {		
		render("resList.jsp");
	}
	public void resList(){	
//		int start=(getParaToInt("start")!=0)?getParaToInt("start"):1;
		int pageNumber=(getParaToInt("pageIndex")!=0)?getParaToInt("pageIndex")+1:1;		
		int pageSize=(getParaToInt("limit")!=0)?getParaToInt("limit"):10;		
		Page<ResModel> menuList= ResModel.dao.paginate(pageNumber,pageSize,getPara("name"));
		Map<String, Object> map=MapUtil.toModel("rows", menuList);
//		map.put("rows",goodsModelList.getList() );
		map.put("results",menuList.getTotalRow());
		map.put("hasError", false);		
		renderJson(map);
		
	}
	public void add() {
		ResModel res =new ResModel();
		setAttr("res", res);
		render("resAdd.jsp");
	}
	
	public void edit() {
		setAttr("res", ResModel.dao.get(getParaToInt()));
		render("resEdit.jsp");
	}
	
	public void view() {
		setAttr("res", ResModel.dao.get(getParaToInt()));
		render("resView.jsp");
	}
	
	@Before(ResourceValidator.class)
	public void save() {
		getModel(ResModel.class).save();
		redirect("/ms/shiro/res");
	}
	
	@Before(ResourceValidator.class)
	public void update() {
		getModel(ResModel.class).update();
		redirect("/ms/shiro/res");
	}
	
	public void delete() {
		ResModel.dao.deleteById(getParaToInt());
		redirect("/ms/shiro/res");
	}
}


